﻿var vm = this.lookupController().getViewModel();

var win = Ext.create('Ext.window.Window', {
    title: 'Department Head(LL5+)',
    height: 600,
    width: 900,
    modal: true,
    layout: {
        type: 'float',
        columns: 1
    },
    buttons: [{
        xtype: "button",
        text: "Comfirm",
        handler: function () {
            var win = this.up("window"),
                refs = win.getReferences();
            //赋值
            var record = refs.grid.getSelection();
            if (record.length > 0) {
                ref.departmentHead.setValue(record[0].get("jlrChineseName"));
                vm.set('NebulogyForm.departmentHeadID', record[0].get("empId"));
                vm.set('NebulogyForm.departmentHeadCDSID', Ext.String.format("{0}({1})", record[0].get("jlrChineseName"), record[0].get("employeeNo")));
            }
            win.close();
        }
    }, {
        xtype: "button",
        text: "Cancel",
        handler: function () {
            this.up("window").close();
        }
    }],
    //主视图内部控件
    referenceHolder: true,
    items: [{
        xtype: 'paigrid',
        reference: 'grid',
        flex: 10,
        enableAdd: false,
        enableDel: false,
        pagingConfigs: {
            displayInfo: true
        },
        enablePaging: false,
        enableSearch: false,
        columns: [{
            text: 'LastName',
            dataIndex: 'lastName',
            flex: 1
        }, {
            text: 'FirstName',
            dataIndex: 'firstName',
            flex: 1
        }, {
            text: 'JLR ChineseName',
            dataIndex: 'jlrChineseName',
            flex: 1
        }, {
            text: 'CDSID',
            dataIndex: 'employeeNo',
            flex: 1
        }, {
            text: 'JLR Department',
            dataIndex: 'Department',
            flex: 1
        }],
        dockedItems: [{
            xtype: 'toolbar',
            dock: 'top',
            items: ['->', {
                xtype: 'textfield',
                width: 200,
                listeners: {
                    change: function (field) {
                        var searchWord = field.getValue().toLowerCase();
                        var store = win.down('grid').store;

                        if (searchWord) {
                            store.filterBy(function (record) {
                                return record.get('lastName').toLowerCase().indexOf(searchWord) != -1 || record.get('employeeNo').toLowerCase().indexOf(searchWord) != -1;
                            });
                        } else {
                            store.clearFilter();
                        }

                    }
                }
            }]
        }],
        store: {
            autoLoad: true,
            fields: [
                { name: 'empId', type: 'string' },
                { name: 'lastName', type: 'string' },
                { name: 'firstName', type: 'string' },
                { name: 'jlrChineseName', type: 'string' },
                { name: 'employeeNo', type: 'string' },
                { name: 'Department', type: 'string' }
            ],
            proxy: {
                type: 'pai.rest',
                ns: PAI.application.userServerUrl,
                requestName: 'ObjectQuery',
                reader: {
                    type: 'json',
                    rootProperty: "result"
                }
            },
            listeners: {
                beforeload: function (store) {
                    //是否是阿里环境
                    var isAli = window.location.href.indexOf('nbl') != -1;

                    var sql = {
                        'registryCenterId': '26e8d725-d27a-41a1-929b-323c75ec7dda',
                        "sqlParam": {
                            '@employeeNo': PAI.Global.getAccount().employee.employeeNo
                        },
                        objectSqlStr: "SELECT t1.bId AS empId ,t2.lastName,t2.firstName, t2.ext_jlrChineseName as jlrChineseName,t2.employeeNo ,t2.ext_subDept as Department"
                            + " FROM orgEmployeeRel t1 "
                            + " JOIN employeeObj t2 ON t1.bId = t2.id "
                            + " JOIN organizationObj t3 ON t1.aId = t3.id "
                            + " JOIN employeeObj t4 on t3.ext_jlrOrgNumber like concat('%', t4.ext_jlrOrgCode, '%') "
                            + " WHERE t2.ext_jlrJobGrade IN('LL2', 'LL3', 'LL4', 'LL5') "
                            + " AND t4.employeeNo = '" + (isAli ? PAI.Global.getAccount().employee.employeeNo : '@employeeNo') + "'"
                    };

                    store.proxy.setExtraParams(sql);
                }
            }
        }
    }],
    listeners: {
        close: function () {
            Ext.getBody().unmask();
        }
    }
}).show();